package cn.com.scooper.impl.xf;


import cn.com.scooper.common.constant.CommonMineConstant;
import cn.com.scooper.common.util.StringUtils;
import cn.com.scooper.dao.xf.MineBasicDataDao;
import cn.com.scooper.pojo.po.MineBasicDataPo;
import cn.com.scooper.service.xf.MineBasicDataService;
import cn.com.scooper.utils.MineCityRequestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static cn.com.scooper.common.constant.CommonMineConstant.CITY_MINE_SAVE_URL;

@Service
public class MineBasicDataServiceImpl implements MineBasicDataService {
    @Autowired
    private MineBasicDataDao mineBasicDataDao;
    @Autowired
    private RestTemplate restTemplate;

    private static final Logger logger = LoggerFactory.getLogger(MineBasicDataServiceImpl.class);

    @Override
    public MineBasicDataPo getMineBasicData(Integer id) {
        return this.mineBasicDataDao.selectByPrimaryKey(id);
    }

    @Override
    public int saveMineBasicData(MineBasicDataPo mineBasicDataPo) {
        return this.mineBasicDataDao.insert(mineBasicDataPo);
    }

    @Override
    public int removeMineBasicData(String stMineId) {
        int i = mineBasicDataDao.deleteByStMineId(stMineId);
        String url = CommonMineConstant.CITY_MINE_DELETE_URL + "/" + stMineId + "v1";
        MineCityRequestUtils.mineAndDeviceReportGet(restTemplate, "删除矿山数据", url);
        return i;
    }

    @Override
    public int updateMineBasicData(MineBasicDataPo mineBasicDataPo) {
        return this.mineBasicDataDao.updateByPrimaryKey(mineBasicDataPo);
    }

    @Override
    public List<MineBasicDataPo> getMineBasicDataList(MineBasicDataPo mineBasicDataPo) {
        List<MineBasicDataPo> list = mineBasicDataDao.selectAll(mineBasicDataPo);
        return list;
    }

    @Override
    public Page<MineBasicDataPo> getMineBasicDataPage(final MineBasicDataPo mineBasicDataPo, int pageNum, int pageSize) {
        // Page<MineBasicDataPo> page = PageHelper.startPage(pageNum,pageSize).doSelectPage(()->mineBasicDataDao.selectAll(mineBasicDataPo)); // usage in jdk 8+
        Page<MineBasicDataPo> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                mineBasicDataDao.selectAll(mineBasicDataPo);
            }
        });//usage in jdk6 or jdk7
        return page;
    }

    @Override
    public void reportMineBasicData() {
        List<MineBasicDataPo> reportMineList = mineBasicDataDao.selectAll(new MineBasicDataPo());
        //过滤出需要上报的矿山基础数据
//        List<MineBasicDataPo> reportMineList = list.stream().filter(m -> StringUtils.isEmpty(m.getStSourceId())).collect(Collectors.toList());
        for (MineBasicDataPo mineBasicDataPo : reportMineList) {
            logger.info("矿山基础数据保存入参{}",mineBasicDataPo.toString());
            MineCityRequestUtils.mineAndDeviceReport(mineBasicDataPo.toString(),restTemplate,"矿山基础信息", CITY_MINE_SAVE_URL);
//            if (response != null){
//                //获取上报后的省级id回填
//                JSONObject dataObj = JSONObject.parseObject(response);
//                String stSourceId = dataObj.getString("stSourceId");
//                if (stSourceId == null){
//                    logger.error("【" + mineBasicDataPo.getMineName() + "】" + "矿山上报返回省级源id异常");
//                    continue;
//                }
//                mineBasicDataPo.setStSourceId(stSourceId);
//                //回填省级id
//                mineBasicDataDao.updateByPrimaryKey(mineBasicDataPo);
//            }
        }
    }
}